using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Linq;
using iTextSharp.text;
namespace PocoReport
{
    public class Text: IPart, IContainerPart
    {
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public TextStyle Style { get; protected set; }
        public IList<IPart> Parts { get; protected set; }
        
        public Text()
        {
            Style = new TextStyle();
            Parts = new List<IPart>();
        }

        public void Load(XNode node)
        {
            XElement element = (XElement)node;

            Style = new TextStyle(element);

            foreach (XNode childNodes in element.Nodes())
            {
                IPart part = PartFactory.GetPart(childNodes);
                if (part != null)
                {
                    Parts.Add(part);
                }
            }
        }

        public virtual XNode Save()
        {
            XElement element = new XElement("Text");
            element.SetAttribValue("Style", Style);
            foreach (IPart part in Parts)
            {
                element.Add(part.Save());
            }
            return element;
        }

        public virtual IElement GetPdfElement(Document document, Style parentStyle, Scope scope)
        {
            
            Phrase phrase = new Phrase();
            Style mergedStyle = PocoReport.Style.Merge(parentStyle, Style);
            foreach (IPart part in Parts)
            {
                phrase.Add(part.GetPdfElement(document, mergedStyle, scope));
            }
            return phrase;
        }
    }
}